package com.ctshk.rpc.visa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.OrderPaymentStatus;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.rpc.visa.entity.VisaOrder;
import com.ctshk.rpc.visa.mapper.VisaOrderMapper;
import com.ctshk.rpc.visa.service.IVisaOrderTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * TODO
 *
 * @author huangxiansheng
 * @date 2021/5/18 11:50 上午
 */
@Slf4j
@DubboService
public class VisaOrderTaskServiceImpl extends ServiceImpl<VisaOrderMapper, VisaOrder> implements IVisaOrderTaskService {

    @Autowired
    VisaOrderMapper visaOrderMapper;

    @Override
    public Result notPaid(Long id) {
        VisaOrder visaOrder = visaOrderMapper.selectOne(new QueryWrapper<VisaOrder>().lambda()
                .eq(VisaOrder::getId, id)
                .eq(VisaOrder::getStatus, OrderPaymentStatus.UN_PAYMENT)
                .eq(VisaOrder::getIsDeleted, IsDeletedCode.NO.getCode()));
        if (visaOrder == null) {
            log.error("【签证订单定时任务-未支付超时】找不到订单,id:{}", id);
            throw new BusinessException(SystemError.VISA_ORDER_25005);
        }
        visaOrder.setStatus(OrderPaymentStatus.ALREADY_CANCEL.getCode());
        visaOrder.setCancelStatus(0);
        visaOrder.setBookingStatus(0);
        visaOrder.setModifiedId(1L);
        visaOrder.setGmtModified(LocalDateTime.now());
        visaOrderMapper.updateById(visaOrder);
        return Result.success();
    }
}
